package com.spic.system.controller;

import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.utils.cache.DataScopeCacheUtils;
import com.spic.system.api.dto.DataScopeDTO;
import com.spic.system.domain.SysDatascope;
import com.spic.system.domain.SysMenu;
import com.spic.system.service.ISysDatascopeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据权限Controller
 *
 * @author spic
 * @date 2022-01-05
 */
@RestController
@RequestMapping("/datascope")
public class SysDatascopeController extends BaseController
{
    @Autowired
    private ISysDatascopeService sysDatascopeService;

    /**
     * 查询数据权限
     */
    @GetMapping("/cache/{menuPath}/{roleId}")
    public Integer cache(@PathVariable("menuPath")String menuPath,@PathVariable("roleId")Long roleId)
    {
        List<DataScopeDTO> list = DataScopeCacheUtils.getCache();
        List<DataScopeDTO> collect = list.stream().filter(i -> menuPath.equals(i.getMeunPath())&& roleId==i.getRoleId()).collect(Collectors.toList());
        if(collect!=null && collect.size()>0){
            return collect.get(0).getDatascope();
        }
        return 0;
    }
    /**
     * 查询数据权限列表
     */
//    @PreAuthorize(hasPermi = "system:role:list")
    @GetMapping("/list")
    public AjaxResult list(SysDatascope sysDatascope)
    {
        List<SysDatascope> list = sysDatascopeService.selectSysDatascopeList(sysDatascope);
        return AjaxResult.success(list);
    }

    /**
     * 导出数据权限列表
     */
//    @PreAuthorize(hasPermi = "system:role:list")
    @Log(title = "数据权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDatascope sysDatascope) throws IOException
    {
        List<SysDatascope> list = sysDatascopeService.selectSysDatascopeList(sysDatascope);
        ExcelUtil<SysDatascope> util = new ExcelUtil<SysDatascope>(SysDatascope.class);
        util.exportExcel(response, list, "数据权限数据");
    }

    /**
     * 获取数据权限详细信息
     */
//    @PreAuthorize(hasPermi = "system:role:list")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysDatascopeService.selectSysDatascopeById(id));
    }

    /**
     * 新增数据权限
     */
//    @PreAuthorize(hasPermi = "system:role:list")
    @Log(title = "数据权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDatascope sysDatascope)
    {
        return toAjax(sysDatascopeService.insertSysDatascope(sysDatascope));
    }

    @GetMapping("selectByRoleIdMenuId")
    @Log(title = "查询菜单权限")
    public AjaxResult selectByItem(SysDatascope sysDatascope)
    {
        if (sysDatascope.getRoleId() == null || sysDatascope.getMeunId() == null)
        {
            return AjaxResult.error("选择的菜单无效");
        }
        return  AjaxResult.success(sysDatascopeService.selectByItem(sysDatascope));
    }

    /**
     * 修改数据权限
     */
//    @PreAuthorize(hasPermi = "system:role:list")
    @Log(title = "数据权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDatascope sysDatascope)
    {
        return toAjax(sysDatascopeService.updateSysDatascope(sysDatascope));
    }

    /**
     * 删除数据权限
     */
//    @PreAuthorize(hasPermi = "system:role:list")
    @Log(title = "数据权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDatascopeService.deleteSysDatascopeByIds(ids));
    }



    @Log(title = "数据权限", businessType = BusinessType.INSERT)
    @PostMapping("/insertUpdate")
    public AjaxResult insertUpdate(@RequestBody SysDatascope sysDatascope)
    {
        List<SysMenu> sysMenuList = sysDatascope.getSysMenuList();
        if(sysMenuList !=null && sysMenuList.size()>0){
            for(SysMenu sysMenu:sysMenuList){

                SysDatascope itemSysDatascope =new SysDatascope();
                itemSysDatascope.setMeunId(sysMenu.getMenuId());
                itemSysDatascope.setRoleId(sysDatascope.getRoleId());
                itemSysDatascope.setMeunPath(sysMenu.getPath());
                itemSysDatascope.setDatascope(sysDatascope.getDatascope());

                SysDatascope item=sysDatascopeService.selectByItem(itemSysDatascope);
                if(item !=null){
                    sysDatascopeService.updateSysDatascope(itemSysDatascope);
                }else{
                    sysDatascopeService.insertSysDatascope(itemSysDatascope);
                }
            }
        }





        return toAjax(1);
    }

}
